## R Script Output -------------------------------------------------------------
# Appendix Figure A1: Top Five Foreign Buyers of U.S. Residential Real Estate, 2010–2020


## Instructions ----------------------------------------------------------------
# Step 1: Adjust MAIN_DIR to where README.txt is located
# Step 2: Run entire script


## setup -----------------------------------------------------------------------
# clean slate
rm(list = ls())
date()

# load packages
pkg <- c("tidyverse",
         "RColorBrewer", 
         "gridExtra")
lapply(pkg, require, character.only = TRUE)

# set main directory
MAIN_DIR <- "~/Dropbox/Research/ISQ-frei-replication/"


## load data -------------------------------------------------------------------
load(file = paste(MAIN_DIR, "nar-df.RData", sep = ""))


## Figure A1 -------------------------------------------------------------------
# set parameters
display.brewer.pal(9, "Set1")
brewer.pal(9, "Set1")

colors <- c(
  "#377EB8", # Canada
  "#E41A1C", # China
  "#FF7F00", # India
  "#4DAF4A", # Mexico
  "black" # UK
)

shapes <- c(16, 17, 15, 4, 7)

axis.title.size <- 16

# plot sales
f.nar.sale <- ggplot(nar,
                     aes(x = year, 
                         y = sale,
                         group = factor(cty),
                         color = factor(cty),
                         shape = factor(cty))) +
  geom_point(size = 3) + 
  geom_line() + 
  scale_color_manual(values = colors) +
  scale_shape_manual(values = shapes) +
  scale_y_continuous("Sales (Billion USD)", 
                     limits = c(0, 34)) +
  scale_x_continuous("Year",
                     breaks = seq(min(nar$year), max(nar$year), by = 1),
                     limits = c(min(nar$year),
                                max(nar$year))) +
  ggtitle("Sales to Top 5 Foreign Buyers") +
  theme_bw() +
  theme(plot.title = element_text(size = axis.title.size + 2,
                                  face = "bold",
                                  hjust = 0.5,
                                  margin = margin(0, 0, 20, 0)),
        axis.title.y = element_text(size = axis.title.size + 2,
                                    margin = margin(0, 20, 0, 0)),
        axis.title.x = element_text(size = axis.title.size + 2,
                                    margin = margin(20, 0, 0, 0)),
        axis.text = element_text(size = axis.title.size - 2),
        strip.text = element_text(size = axis.title.size - 1),
        strip.background = element_blank(),
        panel.grid.minor = element_blank(),
        legend.position = "none")

# annotate
# Canada
f.nar.sale <- f.nar.sale + annotate("text", x = 2015.7, y = 15,
                                    label = "Canada", color = colors[[1]],
                                    size = 5)
# China
f.nar.sale <- f.nar.sale + annotate("text", x = 2017, y = 33.5,
                                    label = "China", color = colors[[2]],
                                    size = 5)
# India
f.nar.sale <- f.nar.sale + annotate("text", x = 2014.3, y = 9,
                                    label = "India", color = colors[[3]],
                                    size = 5)
# Mexico
f.nar.sale <- f.nar.sale + annotate("text", x = 2011, y = 3,
                                    label = "Mexico", color = colors[[4]],
                                    size = 5)
# UK
f.nar.sale <- f.nar.sale + annotate("text", x = 2015, y = 2.2,
                                    label = "UK", color = colors[[5]],
                                    size = 5)

# plot shares
f.nar.share <- ggplot(nar,
                      aes(x = year, 
                          y = share,
                          group = factor(cty),
                          color = factor(cty),
                          shape = factor(cty))) +
  geom_point(size = 3) + 
  geom_line() + 
  scale_color_manual(values = colors) +
  scale_shape_manual(values = shapes) +
  scale_y_continuous("Share", 
                     limits = c(0, 0.34)) +
  scale_x_continuous("Year",
                     breaks = seq(min(nar$year), max(nar$year), by = 1),
                     limits = c(min(nar$year),
                                max(nar$year))) +
  ggtitle("Share of Sales to Top 5 Foreign Buyers") +
  theme_bw() +
  theme(plot.title = element_text(size = axis.title.size + 2,
                                  face = "bold",
                                  hjust = 0.5,
                                  margin = margin(0, 0, 20, 0)),
        axis.title.y = element_text(size = axis.title.size + 2,
                                    margin = margin(0, 20, 0, 0)),
        axis.title.x = element_text(size = axis.title.size + 2,
                                    margin = margin(20, 0, 0, 0)),
        axis.text = element_text(size = axis.title.size - 2),
        strip.text = element_text(size = axis.title.size - 1),
        strip.background = element_blank(),
        panel.grid.minor = element_blank(),
        legend.position = "none")

# add annotations
# Canada
f.nar.share <- f.nar.share + annotate("text", x = 2017, y = 0.14,
                                      label = "Canada", color = colors[[1]],
                                      size = 5)
# China
f.nar.share <- f.nar.share + annotate("text", x = 2015, y = 0.292,
                                      label = "China", color = colors[[2]],
                                      size = 5)
# India
f.nar.share <- f.nar.share + annotate("text", x = 2014.5, y = 0.09,
                                      label = "India", color = colors[[3]],
                                      size = 5)
# Mexico
f.nar.share <- f.nar.share + annotate("text", x = 2013.5, y = 0.038,
                                      label = "Mexico", color = colors[[4]],
                                      size = 5)
# UK
f.nar.share <- f.nar.share + annotate("text", x = 2015, y = 0.021,
                                      label = "UK", color = colors[[5]],
                                      size = 5)

# print
pdf(file = paste(MAIN_DIR, "Figure-A1.pdf", sep = "/"), 
    width = 12, height = 6)
grid.arrange(f.nar.sale, f.nar.share, 
             nrow = 1)
dev.off()

